package com.joycessh.action;

import java.sql.SQLException;
import java.util.Map;

import com.joycessh.dao.UserDao;
import com.joycessh.dao.impl.UserDaoImpl;
import com.joycessh.vo.UpdatePasswordVO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UpdatePasswordAction extends ActionSupport {

	/**
	 * @author joycessh
	 */
	private static final long serialVersionUID = 1L;
	
	private UpdatePasswordVO password;
	private UserDao pDao;
	public UpdatePasswordVO getPassword() {
		return password;
	}
	public void setPassword(UpdatePasswordVO password) {
		this.password = password;
	}
	
	public String execute() {
		
		try {
			pDao = new UserDaoImpl();
			
			ActionContext actionContext = ActionContext.getContext();
		    @SuppressWarnings("unchecked")
			Map<String, String> session = actionContext.getSession();
			String username = session.get("username");
			
			if (username != null) {
				while (pDao.updatePassword(username, password.getOldPassword(), password.getNewPassword())) {
					return "success";
				}
				return "fail";
			} else {
				return "fail";
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return "input";
		}
				
	}

}
